import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store_new'
const _import = require('./import-development')

Vue.use(Router)

function loginHandle (to, from, next) {
  let token = Vue.cookie.get('token')
  if (!token || !/\S/.test(token)) {
    next({ name: 'login' })
  } else {
    store.commit('user/updateId', window.localStorage.getItem('userId'))
    store.commit('user/updateName', window.localStorage.getItem('userName'))
    store.commit('user/updateMobile', window.localStorage.getItem('userMobile'))
    store.commit('user/updateUserType', window.localStorage.getItem('userTpye'))
    next()
  }
}

export default new Router({
  mode: 'hash',
  routes: [
    { path: '/login', component: _import('login/login'), name: 'login', meta: { title: '登录' } },
    { path: '/', component: _import('home/index'), name: 'home', meta: { title: '官网主页' } },
    { path: '/404', component: _import('error/index'), name: '404', meta: { title: '404未找到' } },
    {
      path: '/main',
      component: _import('main/index'),
      name: 'main',
      meta: { title: '主页面' },
      beforeEnter (to, from, next) {
        loginHandle(to, from, next)
      }
    },
    {
      path: '/agency',
      component: _import('agency/layout/index'),
      name: 'agency',
      meta: { title: '代理页面' },
      redirect: { name: 'agency-tabs' },
      children: [
        {
          path: '/agency/tabs',
          component: _import('agency/layout/content-tabs'),
          name: 'agency-tabs',
          redirect: { name: 'ag-overview-sys' },
          meta: { title: '内容需通过tabs展示' },
          children: [
            { path: '/ag/overview-sys', component: _import('agency/overview/index'), name: 'ag-overview-sys', meta: { title: '系统概况' } },
            { path: '/ag/applist', component: _import('agency/applist/index'), name: 'ag-applist', meta: { title: '小程序列表' } },
            // { path: '/ag/ag-system-config', component: _import('agency/config/miniconfig/index'), name: 'ag-system-config', meta: { title: '系统配置' } },
            // { path: '/ag/ag-system-detail/:id', props: true, component: _import('agency/config/miniconfig/detail'), name: 'ag-system-detail', meta: { title: '专题详细页' } },
            { path: '/ag/miniProduct', component: _import('agency/app-manage/mini-product/index'), name: 'ag-app-manage', meta: { title: '应用管理' } },
            { path: '/ag/miniProducPrice', component: _import('agency/app-manage/mini-produc-price/index'), name: 'ag-app-product-price', meta: { title: '套餐管理' } },
            { path: '/ag/miniuser', component: _import('agency/user-manage/index'), name: 'ag-user-manage', meta: { title: '用户管理' } }
          ]
        }
      ],
      beforeEnter (to, from, next) {
        loginHandle(to, from, next)
      }
    },
    {
      path: '/create',
      component: _import('create/index'),
      name: 'create',
      meta: { title: '创建APP' },
      beforeEnter (to, from, next) {
        loginHandle(to, from, next)
      }
    },
    {
      path: '/miniapp/super-store',
      component: _import('miniapp/super-store/layout/index'),
      name: 'miniapp-super-store',
      redirect: { name: 'super-store-content-tabs' },
      meta: { title: '证书配置' },
      children: [
        {
          path: '/miniapp/super-store/content-tabs',
          component: _import('miniapp/super-store/layout/content-tabs'),
          name: 'super-store-content-tabs',
          redirect: { name: 'st-overview-data' },
          meta: { title: '内容需通过tabs展示' },
          children: [
            // 参数说明
            { path: '/super-store/overview-data', component: _import('miniapp/super-store/overview/index'), name: 'st-overview-data', meta: { title: '数据概况' } },
            { path: '/super-store/store-info-index', component: _import('miniapp/super-store/store-info/index'), name: 'st-store-info-index', meta: { title: '门店信息' } },
            { path: '/super-store/store-info-edit', component: _import('miniapp/super-store/store-info/add-or-update'), name: 'st-store-info-edit', meta: { title: '编辑表单' } },
            { path: '/super-store/version', component: _import('version/index'), name: 'st-version', meta: { title: '版本' } },
          ]
        }
      ],
      beforeEnter (to, from, next) {
        loginHandle(to, from, next)
      }
    },
    { path: '*', redirect: { name: '404' } }
  ]
})
